package com.ljm.boot.helloword.letCode;


public class Test02 {
    ListNode listNode;
    public void add(int val){
        ListNode node =  new ListNode(val);
        if(listNode==null){
            listNode = node;
        }else{
            ListNode current = listNode;
            while(current.next!=null){
                current = current.next;
            }
            current.next = node;
        }
    }
    public ListNode getListNode() {
        return listNode;
    }


    public static void main(String[] args) {
        testHacCycleAndSetParam();//测试链表是否成环
        //testAddTwoNumbersAndSetParam();//两数之和
    }


    public static boolean hacCycle(ListNode headNode){
        ListNode node1 = headNode;
        ListNode node2 = headNode;
        while(node1.next!=null&&node2.next!=null){
            node1 = node1.next;
            node2 = node2.next.next;
            System.out.println(node1.val+"-"+node2.val+"-"+headNode.val);
            if(node1==node2){
                return true;
            }
        }
        return false;
    }



    public static ListNode addTwoNumbers(ListNode node1, ListNode node2 ){
        int upCount = 0;
        ListNode node  =  new ListNode(0);
        ListNode next  =  node ;
        while (node1!=null||node2!=null){
            int data1 =  node1 == null ? 0 :node1.val;
            int data2 =  node2 == null ? 0 :node2.val;
            int count = data1+data2+upCount;
            if(count>9){
                count = count-10;
                upCount = 1;
            }

            next.next = new ListNode(count);
            next = next.next;

            if(node1!=null){node1 = node1.next; }
            if(node2!=null){node2 = node2.next; }

        }
        if(upCount>0){
            next.next = new ListNode(upCount);
        }
        return node.next;
    }

    public static void testAddTwoNumbersAndSetParam(){
        Test02 list =  new Test02();
        list.add(9);
        list.add(9);
        list.add(9);
        list.add(9);
        list.add(9);
        list.add(9);
        list.add(9);
        Test02 list2 =  new Test02();
        list2.add(9);
        list2.add(9);
        list2.add(9);
        list2.add(9);
        ListNode node = addTwoNumbers(list.getListNode(),list2.getListNode());
        while (node!=null){
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static  boolean testHacCycleAndSetParam() {
        Test02 list3 = new Test02();
        list3.add(1);
        list3.add(2);
        list3.add(3);
        list3.add(4);
        ListNode headNode = list3.getListNode();
        ListNode headNode1 = headNode;
        while (headNode1.next != null) {
            headNode1 = headNode1.next;
        }
        headNode1.next = headNode;
        return hacCycle(headNode);
    }

}
